#include <stdio.h>
#include "dclcd.h"
#include "tskcfg.h"
#include "bsp.h"
#include "gui_asclib.h"

static void mute(void);
static void power(void);
static void keyt(void);
static void set(void);
static void menu(void);
static void keyret(void);
static void enter(void);
static void down(void);
static void up(void);
static void left(void);
static void right(void);
static void ok(void);
static void volinc(void);
static void voldec(void);
static void key0(void);
static void key1(void);
static void key2(void);
static void key3(void);
static void key4(void);
static void key5(void);
static void key6(void);
static void key7(void);
static void key8(void);
static void key9(void);

static void load(uint8_t nowpage, uint8_t nowfocus);
static void update(uint8_t keyvalue);

static volatile uint8_t focus = 0;
static const uint8_t ssid = 6;
static const uint8_t max_focus = 3; /* 从0开始 */
/* 长度为4的倍数,不足的用gui_asc_null补齐 */
static const char *gui_title_str[8] = {gui_asc_jysz, gui_asc_tlsz, gui_asc_gmjy, gui_asc_swjy, gui_asc_null, gui_asc_null, gui_asc_null, gui_asc_null};

const static GUI_FormKeysStru gui_calimenu_keys = 
{
	.mute =   NULL,
	.power =  NULL,
  .keyt =   NULL,
  .set =    NULL,
  .home =   gui_form_common_home,
  .menu =   NULL,
  .keyret = keyret,
  .enter =  enter,
	.down =   down,
	.up =     up,
	.left =   left,
	.right =  right,
	.ok =     ok,
	.volinc = NULL,
	.voldec = NULL,
	.key0 =   NULL,
	.key1 =   key1,
	.key2 =   key2,
	.key3 =   key3,
	.key4 =   key4,
	.key5 =   NULL,
	.key6 =   NULL,
	.key7 =   NULL,
	.key8 =   NULL,
	.key9 =   NULL,
};

const GUI_FormStru gui_from_calimenu = 
{
	.load    = load,
	.update  = update,
	.keys    = &gui_calimenu_keys,
  .refresh = &gui_maxdelay_refresh_time,
};

static void load(uint8_t nowpage, uint8_t nowfocus)
{
  dc_cmd_change_screen(ssid);
}

static void update(uint8_t keyvalue)
{
  uint8_t remainder;

  remainder = focus % 4;

  dc_comp_showhide(ssid, 7,  remainder-0);
  dc_comp_showhide(ssid, 9,  remainder-1);
  dc_comp_showhide(ssid, 11, remainder-2);
  dc_comp_showhide(ssid, 13, remainder-3);
  
  dc_display_title(ssid, gui_title_str[focus-remainder],   gui_title_str[focus-remainder+1], \
                         gui_title_str[focus-remainder+2], gui_title_str[focus-remainder+3]);
}

static void mute(void)
{
}

static void power(void)
{
}

static void keyt(void)
{
}

static void set(void)
{
}

static void menu(void)
{
}

static void keyret(void)
{
  gui_form_exec(&gui_from_main, 0, 0);
}

static void enter(void)
{
}

static void down(void)
{
  if(focus >= max_focus)
  {
    focus = 0;
  }
  else
  {
    focus++;
  }
}

static void up(void)
{
  if(focus > max_focus)
  {
    focus = max_focus;
  }
  if(focus <= 0)
  {
    focus = max_focus;
  }
  else
  {
    focus--;
  }
}

static void left(void)
{
  uint8_t temp;
  
  if(focus > max_focus)
  {
    focus = 0;
  }
  else if(focus < 4)
  {
    temp = ((max_focus/4)*4) + focus;
    if(temp > max_focus)
    {
      focus = max_focus;
    }
    else
    {
      focus = temp;
    }
  }
  else
  {
    focus -= 4;
  }
}

static void right(void)
{
  uint8_t temp;
  
  temp = focus+4;
  
  if(focus > max_focus)
  {
    focus = max_focus;
  }
  else if(temp <= max_focus)
  {
    focus = temp;
  }
  else
  {
    if(focus < ((max_focus/4)*4))
    {
      focus = max_focus;
    }
    else
    {
      focus %= 4;
    }
  }
}

static void ok(void)
{
  switch(focus)
  {
    case 0:   /* 校验设置 */
      gui_form_exec(&gui_from_paradisp, 8, focus);
      break;
    
    case 1:   /* 调零 */
      gui_form_exec(&gui_from_calzeroready, 0, 0);
      break;
      
    case 2:   /* 挂码 */
      gui_form_exec(&gui_from_calgmready, 0, 0);
      break;
      
    case 3:   /* 实物 */
      gui_form_exec(&gui_from_calswready, 0, 0);
      break;
      
    default:
      focus = 0;
      gui_form_exec(&gui_from_main, 0, 0);
      break;
  }
  
}

static void volinc(void)
{
}

static void voldec(void)
{
}

static void key0(void)
{
}

static void key1(void)
{
  gui_form_exec(&gui_from_paradisp, 7, focus);
}

static void key2(void)
{
  gui_form_exec(&gui_from_calzeroready, 0, 0);
}

static void key3(void)
{
  gui_form_exec(&gui_from_calgmready, 0, 0);
}

static void key4(void)
{
  gui_form_exec(&gui_from_calswready, 0, 0);
}

static void key5(void)
{
}

static void key6(void)
{
}

static void key7(void)
{
}

static void key8(void)
{
}

static void key9(void)
{
}
